package org.dromara.creditcard.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.creditcard.domain.CreditCard;
import org.dromara.creditcard.service.ICreditCardService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.creditcard.domain.CreditBillMonthRecord;
import org.dromara.creditcard.service.ICreditBillMonthRecordService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 信用卡还款的月份标识记录
 *
 * @author Mr.Zhou
 * @date 2023-11-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/credit/billMonthRecord")
public class CreditBillMonthRecordController extends BaseController {

    private final ICreditBillMonthRecordService creditBillMonthRecordService;

    private final ICreditCardService creditCardService;

    /**
     * 查询信用卡还款的月份标识记录列表
     */
    @SaCheckPermission("credit:monthRecord:list")
    @GetMapping("/list")
    public TableDataInfo<CreditBillMonthRecord> list(CreditBillMonthRecord creditBillMonthRecord, PageQuery pageQuery) {
        QueryWrapper<CreditBillMonthRecord> qw = new QueryWrapper<>();

        if(creditBillMonthRecord.getCreditCardId()!= null){
            qw.eq("credit_card_id", creditBillMonthRecord.getCreditCardId());
        }
        qw.orderByDesc("id");

        TableDataInfo<CreditBillMonthRecord> td = PageUtils.buildDataInfo(creditBillMonthRecordService.page(PageUtils.buildPage(),qw));
        List<CreditBillMonthRecord> list = td.getRows();
        return td;
    }

    /**
     * 导出信用卡还款的月份标识记录列表
     */
    @SaCheckPermission("credit:monthRecord:export")
    @Log(title = "信用卡还款的月份标识记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CreditBillMonthRecord creditBillMonthRecord, HttpServletResponse response) {
        List<CreditBillMonthRecord> list = creditBillMonthRecordService.list();
        ExcelUtil.exportExcel(list, "信用卡还款的月份标识记录", CreditBillMonthRecord.class, response);
    }

    /**
     * 获取信用卡还款的月份标识记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("credit:monthRecord:query")
    @GetMapping("/{id}")
    public R<CreditBillMonthRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(creditBillMonthRecordService.getById(id));
    }

    /**
     * 新增信用卡还款的月份标识记录
     */
    @SaCheckPermission("credit:monthRecord:add")
    @Log(title = "信用卡还款的月份标识记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CreditBillMonthRecord creditBillMonthRecord) {

        QueryWrapper<CreditBillMonthRecord> qw = new QueryWrapper<>();
        qw.eq("credit_card_id", creditBillMonthRecord.getCreditCardId());
        qw.eq("month_code", creditBillMonthRecord.getMonthCode());
        long monthRecordCount = creditBillMonthRecordService.count(qw);
        if(monthRecordCount > 0){
            return  R.fail("已存在该月份账单");
        }
        creditBillMonthRecord.setCreateTime(new Date());
        boolean bl = creditBillMonthRecordService.save(creditBillMonthRecord);
        if(bl){
            CreditCard creditCard = creditCardService.getById(creditBillMonthRecord.getId());
            if(creditCard != null){
                int ret = creditCardService.splitBill(creditBillMonthRecord,creditCard);
                return toAjax(ret);
            }
        }
        return toAjax(bl);
    }

    /**
     * 账单拆分
     */
    @SaCheckPermission("credit:monthRecord:split")
    @Log(title = "账单拆分", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/split/bill/{id}")
    public R<Void> splitBill(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        CreditBillMonthRecord monthRecord = creditBillMonthRecordService.getById(id);
        if(monthRecord != null){
            CreditCard creditCard = creditCardService.getById(monthRecord.getId());
            if(creditCard != null){
                int ret = creditCardService.splitBill(monthRecord,creditCard);
                return toAjax(ret);
            }
        }
        return R.fail("不存在");
    }

    /**
     * 修改信用卡还款的月份标识记录
     */
    @SaCheckPermission("credit:monthRecord:edit")
    @Log(title = "信用卡还款的月份标识记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CreditBillMonthRecord creditBillMonthRecord) {
        return toAjax(creditBillMonthRecordService.updateById(creditBillMonthRecord));
    }

    /**
     * 删除信用卡还款的月份标识记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("credit:monthRecord:remove")
    @Log(title = "信用卡还款的月份标识记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(creditBillMonthRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
